/*
 *  Copyright (C) 2011  Clint Priest
 *
 *  This file is part of Snap Graph.
 *
 *  Snap Graph is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Snap Graph is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Snap Graph.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

LoadScript('chrome://SnapGraph/content/Browser/Graph.js');

var GraphWindow = Class.create(XulPanel, {
	UserSelections: [ ],
	DataSelections: [ ],
	
	initialize: function($super) {
		$super();
		this.Title = 'Graph 1';
		this.Width = 506;
		this.Height = 300;
		this.Graph = new Graph(500, 300);
		this.ContentElement.appendChild(this.Graph.Paper.canvas);
	},
	/* Adds a user selection to the graph window */
	AddSelection: function(UserSelection) {
		UserSelection.Highlight();
		this.UserSelections.push(UserSelection);
		this.GatherData();
		this.UpdateGraph();
	},
	GatherData: function() {
		var DataSelections = { };
		
		this.UserSelections.forEach( function(UserSelection) {
			UserSelection.SelectedElements.forEach( function(elem) {
				var RowID = elem.SnapRowHeader.identify();
				if(!DataSelections[RowID]) {
					DataSelections[RowID] = { 
						Data: 	{ },
						Title: 	elem.SnapRowHeader.textContent,
					}
				}
				DataSelections[RowID].Data[elem.SnapColHeader.textContent] = elem.textContent;
			});
		});
		console.log(DataSelections);
		this.DataSelections = Object.values(DataSelections);
	},
	UpdateGraph: function() {
		this.Graph.DataSelections = this.DataSelections;
	},
	OnResize: function(rect) {
		this.Graph.SetSize(rect.width, rect.height);
	}
} );